package com.lefevre.cms.web.action.user;

import com.lefevre.cms.bean.user.UserDynamic;
import com.lefevre.cms.utils.UUIDUtil;
import com.lefevre.cms.utils.Verification;
import org.springframework.stereotype.Component;

/**
 * 用户动态管理
 */
@Component("userDynamicManage")
public class UserDynamicManage {

    /**
     * 取得用户动态Id的用户Id(后N位)
     *
     * @param userDynamicId 用户动态Id
     * @return
     */
    public int getUserDynamicUserId(String userDynamicId) {
        String after_userId = userDynamicId.substring(userDynamicId.length() - 4);
        return Integer.parseInt(after_userId);
    }

    /**
     * 生成用户动态Id
     * 用户动态Id由32位uuid+1位用户Id后4位组成
     *
     * @param userId 用户Id
     * @return
     */
    public String createUserDynamicId(Long userId) {
        //选取得后N位用户Id
        String afterUserId = String.format("%04d", userId % 10000);
        String id = UUIDUtil.getUUID32() + afterUserId;
        return id;
    }

    /**
     * 校验用户动态Id
     * 用户动态Id要先判断是否36位并且最后4位是数字
     *
     * @param userDynamicId 用户动态Id
     * @return
     */
    public boolean verificationUserDynamicId(String userDynamicId) {
        if (userDynamicId != null && !"".equals(userDynamicId.trim())) {
            if (userDynamicId.length() == 36) {
                String after_userId = userDynamicId.substring(userDynamicId.length() - 4);
                boolean verification = Verification.isPositiveIntegerZero(after_userId);//数字
                if (verification) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 生成用户动态对象
     *
     * @return
     */
    public Object createUserDynamicObject(UserDynamic userDynamic) {
        return userDynamic;
    }

}
